import router from './router.ts'

// https://www.npmjs.com/package/nprogress
// @ts-ignore
import _nprogress from 'nprogress'

// https://router.vuejs.org/zh/guide/advanced/navigation-guards.html

// 路由前置判断，在这里更改目标路由，做路由拦截
router.beforeEach(async (to, from, next) => {
  console.log('beforeEach')
  console.log('to', to)
  console.log('from', from)
  _nprogress.start()
  /**
   * Route control:
   * - With `next`: Call `next()` only
   * - Without `next`: Return `true`/`false` or path
   */
  return next()
})

// 路由全局解析，在这里获取路由信息，在最后跳转前获取相应的前置条件(比如获取摄像头等用户访问权限)
router.beforeResolve(async (to, from) => {
  console.log('beforeResolve')
  console.log('to', to)
  console.log('from', from)
  return true
})

// 路由后置守卫
// navigation failures failure:  https://router.vuejs.org/zh/guide/advanced/navigation-failures.html
// import { NavigationFailureType, isNavigationFailure } from 'vue-router'
router.afterEach((to, from, failure) => {
  console.log('afterEach')
  console.log('to', to)
  console.log('from', from)
  console.log('failure', failure)
  _nprogress.done()
  /**
    if (failure) {
      if (isNavigationFailure(failure, NavigationFailureType.aborted)) {
        // 给用户显示一个小通知
        showToast('You have unsaved changes, discard and leave anyway?')
      }
    }
   */
})

export default router
